/**
 * ControladoraBitacora.java
 */

 /**
 *  AdSec: Administrador de Secundarias
 *  Copyright (C) 2009 Daniela Ramírez Chaves (dannyrch@gmail.com)
 *  Copyright (C) 2009 Leonel Salazar Valverde (leo87sv@gmail.com)
 *
 *  This file is part of AdSec.
 *  AdSec is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AdSec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AdSec.  If not, see <http://www.gnu.org/licenses/>.
 */

package adsec.negocios;

import adsec.entidades.Bitacora;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;


public abstract class ControladoraBitacora {
    private LinkedList<Bitacora> colaMensajes;
    private Semaphore semaforoCola;
    private Semaphore semaforoHilo;

    public ControladoraBitacora() {
        this.colaMensajes = new LinkedList<Bitacora>();
        this.semaforoCola = new Semaphore(1, true);
        this.semaforoHilo = new Semaphore(0, true);
    }

    public void agregarRegistroBitacora(Bitacora registro) {
        try{
            this.getSemaforoCola().acquire();
            getColaMensajes().addLast(registro);
            this.getSemaforoCola().release();
            //indicar al hilo que hay al menos un mensaje por enviar
            this.getSemaforoHilo().release();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }//fin de agregarRegistroBitacora

    public abstract void escribirRegistroBitacora(Bitacora registro);

    public abstract void forzarEnvioBitacora();

    /**
     * @return the colaMensajes
     */
    public LinkedList<Bitacora> getColaMensajes() {
        return colaMensajes;
    }

    /**
     * @param colaMensajes the colaMensajes to set
     */
    public void setColaMensajes(LinkedList<Bitacora> colaMensajes) {
        this.colaMensajes = colaMensajes;
    }

    /**
     * @return the semaforoCola
     */
    public Semaphore getSemaforoCola() {
        return semaforoCola;
    }

    /**
     * @param semaforoCola the semaforoCola to set
     */
    public void setSemaforoCola(Semaphore semaforoCola) {
        this.semaforoCola = semaforoCola;
    }

    /**
     * @return the semaforoHilo
     */
    public Semaphore getSemaforoHilo() {
        return semaforoHilo;
    }

    /**
     * @param semaforoHilo the semaforoHilo to set
     */
    public void setSemaforoHilo(Semaphore semaforoHilo) {
        this.semaforoHilo = semaforoHilo;
    }

}//fin de clase ControladoraBitacora
